<?php
  class StugspController extends Controller{
           public $isActivated = 2;
    public function accessRules()
		 {
			return array(
				array(
					'allow',  // allow all users to access 'index' and 'view' actions.
					'expression'=>'$user->rights == 1',
				),
				array(
					'deny',  // deny all users
					'users'=>array('*'),
					'message'=>'You do not have access to this page ! ! ',
				),
			);
		 }
      public function actionIndex(){
		    
		  
	  }
	  public function actionAddgsp(){
		   $this->breadcrumbs=array(
               'GSP和综合素质'=>array('addgsp'),
               '添加GSP',
             );
		  
		  if(Yii::app()->request->requestType == 'POST'){
			  $this->dealAddGsp();
		  }
		  $gsp_gz = array();
		  $gsp_sfs = array();
		  $gsp_zzxx = array();
		  $gsps = Gspitem::model()->findAll(array('order'=>'plateform'));
		  foreach($gsps as $g){
			 
			 switch($g->plateform){
				 case '师范生技能竞赛': $gsp_sfs[] = $g;break;
				 case '学生社团自主学习': $gsp_zzxx[] = $g;break;
				 case '工作及社会实践': $gsp_gz[] = $g;break;
				 
			 }
		 }
		 
		  $this->render("addgsp",array('gsp_sfs'=>$gsp_sfs,'gsp_zzxx'=>$gsp_zzxx,'gsp_gz'=>$gsp_gz));
	  }
	  protected function dealAddGsp(){
		    $request = Yii::app()->request;
		    $stuGsp  = new Stugspitem;
			$gsp = Gspitem::model()->findByPk($request->getParam('gspItemId'));
			if(!$gsp){
				Yii::app()->user->setFlash('add','非法的请求');
           		$this->redirect(array('stugsp/addgsp'));	
			}else{
			 $stuGsp->uId  = Yii::app()->user->getId();
			 $stuGsp->gspItemId = $gsp->gspItemId;
			 $stuGsp->module = $gsp->module;
			 $stuGsp->name = $gsp->name;
			 $stuGsp->plateform = $gsp->plateform;
			 $stuGsp->level = $request->getParam('level');
			 $stuGsp->attendtime = $request->getParam('attendtime');
			 $stuGsp->prize = $request->getParam('prize');
			 $stuGsp->detail = $request->getParam('detail');
			 $stuGsp->credit = $request->getParam('credit');
			 $stuGsp->addtime = date('Y-m-d H:i:s');
			 $stuGsp->condition  = 1;//1 ---- 未审核 2 ---- 未通过 3 --- 已通过
			 if($stuGsp->save()){
				 Yii::app()->user->setFlash('add','添加成功，请到我的GSP查看');
			 }else{
				 $this->logAr($stuGsp);
				  Yii::app()->user->setFlash('add','添加失败');
			 }
			
			}
			
	  }
	   public function actionMygsp(){
		    $this->breadcrumbs=array(
               'GSP和综合素质'=>array('addgsp'),
               '我的GSP',
             );
		  $total = 0; 
		  $gsp_gz = array();
		  $gsp_sfs = array();
		  $gsp_zzxx = array();
		  $stuGsps  = Stugspitem::model()->findAll('uId = :u',array(':u'=>Yii::app()->user->getId()));
		   foreach($stuGsps as $g){
			   // 1 ---- 未审核 2 ---- 未通过 3 --- 已通过
			 if($g->condition == 3){
				 
				 $total += $g->credit;
			 }
			 switch($g->plateform){
				 case '师范生技能竞赛': $gsp_sfs[] = $g;break;
				 case '学生社团自主学习': $gsp_zzxx[] = $g;break;
				 case '工作及社会实践': $gsp_gz[] = $g;break;
				 
			 }
		 }
		  $this->render("mygsp",array('gsp_sfs'=>$gsp_sfs,'gsp_zzxx'=>$gsp_zzxx,'gsp_gz'=>$gsp_gz,'total'=>$total));
	  }
	  public function actionDeletegsp(){
		   if(Yii::app()->request->isAjaxRequest){
			 $id = $_POST['stuGspItemId'];
			 $gsp = Stugspitem::model()->findByPk($id);
			 if($gsp){
				 // 通过审核之后，无法删除
				if($gsp->condition !== '3'){ 
					 if($gsp->delete()){
						 echo json_encode(array('code'=>200,'msg'=>'删除成功')); 
					 }else{
						 $this->logAr($gsp);
						 echo json_encode(array('code'=>501,'msg'=>'删除失败')); 
					 }
				}
			 }else{
				 
				 echo json_encode(array('code'=>404,'msg'=>'没有找到这个条目')); 
			 }
		 }else{
			 echo '非法的操作';
			 
		 }
		  
	  }
	  public function actionAddinte(){
		   $this->breadcrumbs=array(
               'GSP和综合素质'=>array('addgsp'),
               '添加综合素质',
             );
		  
		  if(Yii::app()->request->requestType == 'POST'){
			  $this->dealAddinte();
		  }
		  $intes = Inteitem::model()->findAll();
		  $inte_qspy = array();
		  $inte_shsj = array();
		  $inte_ycjy = array();
		  $inte_zyyy  =array();
		  foreach($intes as $i){
			  
			  switch($i->module){
				  
				  case '养成教育':$inte_ycjy[] = $i;break;
				  case '情商培养':$inte_qspy[] = $i;break;
				  case '社会实践':$inte_shsj[] = $i;break;
				  case '专业应用':$inte_zyyy[] = $i;break;
			  }
		  }
		  $this->render("addinte",array('inte_qspy'=>$inte_qspy,'inte_ycjy'=>$inte_ycjy,'inte_shsj'=>$inte_shsj,'inte_zyyy'=>$inte_zyyy));
		  
	  }
	  protected function  dealAddinte(){
		  $request = Yii::app()->request;
		  $inte = Inteitem::model()->findByPk($request->getParam('inteItemId'));
		  if(!$inte){
			  Yii::app()->user->setFalsh('add','非法请求');
		  }else{
			  $stuInte = new Stuinteitem;
			  $stuInte->uId = Yii::app()->user->getId();
			  $stuInte->inteItemId = $inte->inteItemId;
			  $stuInte->name = $inte->name;
			  $stuInte->module = $inte->module;
			  $stuInte->content = $inte->content;
			  $stuInte->term = $request->getParam('term');
			  $stuInte->detail = $request->getParam('detail');
			  $stuInte->credit = $request->getParam('credit');
			  $stuInte->addtime = date('Y-m-d H:i:s');
			  $stuInte->condition = 1;//1 ---- 未审核 2 ---- 未通过 3 --- 已通过
			  if($stuInte->save()){
				  Yii::app()->user->setFlash('add','添加成功，请到我的综合素质查看');
			  }else{
				  Yii::app()->user->setFlash('add','添加成功，请到我的综合素质查看');
			  }
		  }
		  $this->redirect(array('stugsp/addinte'));
		  
	  }
	   public function actionMyinte(){
		    $this->breadcrumbs=array(
               'GSP和综合素质'=>array('addgsp'),
               '我的综合素质',
             );
		   
		   $stuIntes = Stuinteitem::model()->findAll('uId =  :u',array(':u'=>Yii::app()->user->getId()));
              $total = 0;
			  $inte_qspy = array();
			  $inte_shsj = array();
			  $inte_ycjy = array();
			  $inte_zyyy  =array();
		  foreach($stuIntes as $i){
			  // 1 ---- 未审核 2 ---- 未通过 3 --- 已通过
			  if($i->condition == 3){
				  
				  $total += $i->credit;
				  
			  }
			  switch($i->module){
				  
				  case '养成教育':$inte_ycjy[] = $i;break;
				  case '情商培养':$inte_qspy[] = $i;break;
				  case '社会实践':$inte_shsj[] = $i;break;
				  case '专业应用':$inte_zyyy[] = $i;break;
			  }
		  }
		 $this->render("myinte",array('total'=>$total,'inte_qspy'=>$inte_qspy,'inte_ycjy'=>$inte_ycjy,'inte_shsj'=>$inte_shsj,'inte_zyyy'=>$inte_zyyy));
		  
	  }
	  public function actionDeleteinte(){
		  if(Yii::app()->request->isAjaxRequest){
			 $id = $_POST['stuInteItemId'];
			 $inte = Stuinteitem::model()->findByPk($id);
			 if($inte){
				if($inte->condition !== 3){
					 if($inte->delete()){
						 echo json_encode(array('code'=>200,'msg'=>'删除成功')); 
					 }else{
						 $this->logAr($inte);
						 echo json_encode(array('code'=>501,'msg'=>'删除失败')); 
					 }
				}
			 }else{
				 
				 echo json_encode(array('code'=>404,'msg'=>'没有找到这个条目')); 
			 }
		 }else{
			 echo '非法的操作';
			 
		 }
		  
	  }
	  
  }
?>